<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\inspection\controller;

use app\common\controller\Admin;
use app\inspection\controller\api\Mission as MissionApi;
use app\admin\controller\ajax\Library as LibarayAjax;
use app\inspection\controller\api\Item;
/**
 * 巡检任务管理
 * @package app\rescue
 */
class Mission  extends Admin

{
    
    protected $api;
    protected $property;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new MissionApi();
		$this->property = new LibarayAjax();
	}
    /**
     * 首页
     * @return Json/View
     */
   	public function index() {
   	  if(IS_AJAX){
   	  	$itemList = $this->api->missionList();
   	  	return $itemList;
   	  }
   	  //单位列表
   	  $enterpriseList = model('basic/Enterprise')->column('fullName','id');
   	  $this->assign('enterpriseList',$enterpriseList);
      return $this->fetch();
 	}
 	

 	/**
 	 * 巡检项目查询
 	 * @return Json
 	 */
 	public function search() {
 		if(IS_AJAX){
 			$itemList = $this->api->search(input('keyword'),input('enterpriseId'));
 			return $itemList;
 		}
 	}
 	
 	
 	/**
 	 * 添加巡检项目
 	 * @return Json/View
 	 */
 	public function add(){
 		if(IS_POST){
 			$data = input('post.');
 			if(isset($data['isValid'])){
 				$data['isValid'] = 1;
 			}else{
 				$data['isValid'] = 0;
 			}
 			return $this->api->add($data);
 		}
 		//单位列表
 		$enterpriseList = model('basic/Enterprise')->column('fullName','id');
 		$this->assign('enterpriseList',$enterpriseList);
 		//范围列表
 		$positionType = $this->property->getPositionType()->getData();
 		foreach($positionType as &$item){
 			$item['area'] = $this->property->getAreaType($item['subsystemNo'])->getData();
 		}
 		$this->assign('positionType',$positionType);
 		//负责人列表
 		$chargePersonList = model('user')->column('userName','id');
 		$this->assign('chargePersonList' ,$chargePersonList);
 		
 		return $this->fetch();
 	}
 	
 	/**
 	 * 编辑巡检项目
 	 * @param string $craftId
 	 * @return Json/View
 	 */
 	public function edit($missionId = null){
 		if(IS_POST){
 			$data = input('post.');
 			if(isset($data['isValid'])){
 				$data['isValid'] = 1;
 			}else{
 				$data['isValid'] = 0;
 			}
 			return $this->api->edit($data,$missionId);
 		}
 		//单位列表
 		$enterpriseList = model('basic/Enterprise')->column('fullName','id');
 		$this->assign('enterpriseList',$enterpriseList);
 		//范围列表
 		$positionType = $this->property->getPositionType()->getData();
 		foreach($positionType as &$item){
 			$item['area'] = $this->property->getAreaType($item['subsystemNo'])->getData();
 		}
 		$this->assign('positionType',$positionType);
 		//负责人列表
 		$chargePersonList = model('user')->column('userName','id');
 		$this->assign('chargePersonList' ,$chargePersonList);
 		//巡检任务
 		$mission = model('Mission')->find($missionId);
 		$this->assign('info',$mission);
 		return $this->fetch();
 		
 	}
 	
 	/**
 	 * 删除巡检项目
 	 * @return Json
 	 */
 	public function del($missionId = null) {
 		if(IS_AJAX){
 			return $this->api->del($missionId);
 		}
 	}
 	
 	/**
 	 * 配置巡检任务
 	 * @param string $missionId
 	 * @return Json/View
 	 */
 	public function configItem($missionId = null){
 		//巡检任务
 		if(IS_POST){
 			$data = input('post.');
 			return $this->api->config($data,$missionId);
 		}
 		$mission = model('Mission')->find($missionId);
 		$this->assign('info',$mission);
 		$itemIds = [];
 		foreach($mission['items'] as $value){
 			$itemIds[] = $value['id'];
 		}
 		$this->assign('itemIds',$itemIds);
 		//巡检项目列表
 		$itemApi = new Item();
 		$itemList = $itemApi->itemList($mission['enterpriseId'])->getData();
 		$this->assign('itemList',$itemList['data']);
 		return $this->fetch();
 	}
 	
 	public function detach($missionId = null,$itemId = null){
 		if(IS_AJAX){
 			return $this->api->detach($missionId,$itemId);
 		}
 	}
}
